package com.sooncode.project.focusservice.service;

import com.sooncode.project.core.finder.Finder;
import com.sooncode.project.core.finder.OType;
import com.sooncode.project.core.finder.Page;
import com.sooncode.project.core.finder.Sort;
import com.sooncode.project.focusservice.model.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CourseService {
    @Autowired
    CourseSettingService courseSettingService;
    public boolean add(Map map){
        Course course=new Course();
        course.add(map);
        return true;
    }
    public boolean modify(String id,Map map){
        Course course=getById(id);
        course.modify(map);
        courseSettingService.asyncItemName("");
        return true;
    }
    public boolean delete(String id){
        Course course=getById(id);
        course.delete();
        return true;
    }
    public Page<Course> getPageList( int pageSize, int pageIndex, Map map) {
        String name = (String) map.get("name");
        map.remove("name");
        return new Finder<>(Course.class)
                .byField("name",name, OType.contains)
                .and(map)
                .page(pageSize, pageIndex, Sort.ASC("difficulty"));
    }
    public Course getById(String id){
        return new Finder<>(Course.class).byId(id);
    }

    public List<Course> getCourseList() {
        return new Finder<>(Course.class).list(Sort.ASC("difficulty"));
    }
}
